const Router = require('@koa/router');
const orderController = require('../controllers/order.controller');
const { checkAuth } = require('../middlewares/auth');

const router = new Router({
  prefix: '/api/v1/orders'
});

/**
 * @swagger
 * /api/v1/orders:
 *   post:
 *     tags: [订单管理]
 *     summary: 创建订单
 *     description: 为当前用户创建新的订单
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - type
 *               - amount
 *               - paymentMethod
 *             properties:
 *               type:
 *                 type: string
 *                 enum: [recharge, purchase, service, other]
 *                 description: 订单类型（充值/购买/服务/其他）
 *                 example: "purchase"
 *               amount:
 *                 type: number
 *                 description: 订单金额
 *                 example: 99.99
 *               points:
 *                 type: integer
 *                 description: 订单涉及的积分数量
 *                 example: 1000
 *               items:
 *                 type: array
 *                 description: 订单项目列表
 *                 items:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                       description: 商品/服务ID
 *                       example: "prod_001"
 *                     name:
 *                       type: string
 *                       description: 商品/服务名称
 *                       example: "高级会员月卡"
 *                     quantity:
 *                       type: integer
 *                       description: 数量
 *                       example: 1
 *                     price:
 *                       type: number
 *                       description: 单价
 *                       example: 99.99
 *               paymentMethod:
 *                 type: string
 *                 enum: [wechat, alipay, bank, points, other]
 *                 description: 支付方式（微信/支付宝/银行卡/积分/其他）
 *                 example: "wechat"
 *     responses:
 *       201:
 *         description: 订单创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "order_001"
 *                 orderNumber:
 *                   type: string
 *                   example: "202401150001"
 *                 message:
 *                   type: string
 *                   example: "订单创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 */
router.post('/', checkAuth, orderController.createOrder);

/**
 * @swagger
 * /api/v1/orders:
 *   get:
 *     tags: [订单管理]
 *     summary: 获取订单列表
 *     description: 获取当前用户的所有订单（支持分页和筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [recharge, purchase, service, other]
 *         description: 订单类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, paid, cancelled, refunded, completed]
 *         description: 订单状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 orders:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "order_001"
 *                       orderNumber:
 *                         type: string
 *                         example: "202401150001"
 *                       type:
 *                         type: string
 *                         example: "purchase"
 *                       amount:
 *                         type: number
 *                         example: 99.99
 *                       status:
 *                         type: string
 *                         example: "paid"
 *                       createdAt:
 *                         type: string
 *                         format: date-time
 *                       payments:
 *                         type: array
 *                         items:
 *                           type: object
 *                           properties:
 *                             method:
 *                               type: string
 *                               example: "wechat"
 *                             amount:
 *                               type: number
 *                               example: 99.99
 *                             status:
 *                               type: string
 *                               example: "success"
 *       401:
 *         description: 未认证
 */
router.get('/', checkAuth, orderController.getOrders);

/**
 * @swagger
 * /api/v1/orders/{id}:
 *   get:
 *     tags: [订单管理]
 *     summary: 获取订单详情
 *     description: 获取指定订单的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 订单ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "order_001"
 *                 orderNumber:
 *                   type: string
 *                   example: "202401150001"
 *                 type:
 *                   type: string
 *                   example: "purchase"
 *                 amount:
 *                   type: number
 *                   example: 99.99
 *                 points:
 *                   type: integer
 *                   example: 1000
 *                 status:
 *                   type: string
 *                   example: "paid"
 *                 items:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "prod_001"
 *                       name:
 *                         type: string
 *                         example: "高级会员月卡"
 *                       quantity:
 *                         type: integer
 *                         example: 1
 *                       price:
 *                         type: number
 *                         example: 99.99
 *                 payments:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       method:
 *                         type: string
 *                         example: "wechat"
 *                       amount:
 *                         type: number
 *                         example: 99.99
 *                       status:
 *                         type: string
 *                         example: "success"
 *                       transactionId:
 *                         type: string
 *                         example: "wx202401150001"
 *                       paidAt:
 *                         type: string
 *                         format: date-time
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 updatedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       404:
 *         description: 订单不存在
 */
router.get('/:id', checkAuth, orderController.getOrder);

/**
 * @swagger
 * /api/v1/orders/{id}/cancel:
 *   post:
 *     tags: [订单管理]
 *     summary: 取消订单
 *     description: 取消未支付的订单
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 订单ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               reason:
 *                 type: string
 *                 description: 取消原因
 *                 example: "计划有变"
 *     responses:
 *       200:
 *         description: 取消成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "订单取消成功"
 *       400:
 *         description: 订单无法取消（已支付或已完成）
 *       401:
 *         description: 未认证
 *       404:
 *         description: 订单不存在
 */
router.post('/:id/cancel', checkAuth, orderController.cancelOrder);

/**
 * @swagger
 * /api/v1/orders/stats:
 *   get:
 *     tags: [订单管理]
 *     summary: 获取订单统计
 *     description: 获取用户订单的统计信息
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 stats:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       status:
 *                         type: string
 *                         example: "paid"
 *                       count:
 *                         type: integer
 *                         example: 10
 *                       totalAmount:
 *                         type: number
 *                         example: 999.90
 *                 summary:
 *                   type: object
 *                   properties:
 *                     totalOrders:
 *                       type: integer
 *                       example: 50
 *                     totalAmount:
 *                       type: number
 *                       example: 4999.50
 *                     totalPoints:
 *                       type: integer
 *                       example: 5000
 *       401:
 *         description: 未认证
 */
router.get('/stats', checkAuth, orderController.getUserOrderStats);

/**
 * @swagger
 * /api/v1/orders/date-range:
 *   get:
 *     tags: [订单管理]
 *     summary: 按日期范围查询订单
 *     description: 获取指定日期范围内的订单列表
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: startDate
 *         required: true
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期（YYYY-MM-DD）
 *         example: "2024-01-01"
 *       - in: query
 *         name: endDate
 *         required: true
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期（YYYY-MM-DD）
 *         example: "2024-01-15"
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [recharge, purchase, service, other]
 *         description: 订单类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, paid, cancelled, refunded, completed]
 *         description: 订单状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 20
 *                 orders:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Order'
 *                 summary:
 *                   type: object
 *                   properties:
 *                     totalAmount:
 *                       type: number
 *                       example: 1999.80
 *                     orderCount:
 *                       type: integer
 *                       example: 20
 *       400:
 *         description: 日期范围无效
 *       401:
 *         description: 未认证
 */
router.get('/date-range', checkAuth, orderController.getOrdersByDateRange);

module.exports = router; 